<?php

/*
Plugin Name: Services Custom Posts
Plugin URI:
Description: Plugin to create custom post type of Services for Soot Seekers.
Version: 1.0
Author: Kyle Finley
Author URI: http://www.page110.com/
License: GPL2
*/

// Defines
define( 'SERVICES_META_KEY', 'services_details' );
define( 'SERVICES_META_ORDER', 'services_order');
 
// Includes

require_once ( 'include/services.php' );
require_once ( 'include/services-template-tags.php' );

 
// On plug-in activation
 
register_activation_hook( __FILE__, 'services_activate'); 
 
// Wordpress hooks

add_action( 'init','services_init' );

add_action( 'add_meta_boxes', 'services_add_metabox', 10, 2 );
add_action( 'save_post', 'soot_seekers_service_on_post_save', 10, 2 );
add_action( 'wp_enqueue_scripts', 'add_my_services_stylesheet' );
 
// callback function for template redirect to add in CSS 
function add_my_services_stylesheet() {
	wp_enqueue_style('services-css', plugins_url('css/services.css', __FILE__), array(), '1.0');
}

//callback function run on plugin activation 
function services_activate() {
	 
	if( version_compare( get_bloginfo( 'version' ), '3.0', '<') ) {
		 
		deactivate_plugins( __FILE__ );
		
	} else {
	
		flush_rewrite_rules();
		
	}
 
}

// callback function for init hook
function services_init() {
	
	// register the portfolio custom post type
	soot_seekers_register_services();
		
}

// Create custom meta boxes

function services_add_metabox( $post_type, $post ) {
	
	add_meta_box( 'services_meta',			// CSS ID of the <div> that wraps the metabox
                      'Soot Seekers Service',		// title of the meta box
                      'services_display_metabox',	// callback for rendering metabox content
                      'service',			// post type for metabox display (could also be 'post' or 'custom post type')
                      'normal',				// context - part of the page where the meta will show
                      'high'				// priority of metabox display related to other metaboxes on the page
                      );
	
	// display portfolio metabox	  
	function services_display_metabox( $post ) {
		
		// fetch post-meta data for this post, if it exists
		$services_input = get_post_meta( $post->ID, SERVICES_META_KEY, true );
                $services_order = get_post_meta( $post->ID, SERVICES_META_ORDER, true);
		
		// if not, initialize with defaults
		if( $services_input == false ) {
			
			$services_input = array();
			//$services_input['title'] = 'Same as Title at the top.';
			$services_input['image'] = 'Copy Featured Image URL';
			$services_input['description'] = 'Enter a short description of the service.';
			
		}
		
		// convert portfolio input array to variables
		extract( $services_input );
	
		// display the form
		include 'interface/services-metabox.php';
		
	}
	
}

// run when a post is saved
function soot_seekers_service_on_post_save( $post_id, $post ) {

	// bail on autosave
	if( $post->post_type == 'revision' ) {
	
		return;
		
	}
	
	if( isset( $_POST['services'] ) ) {
		
		$services_input = $_POST['services'];
		
		$services_input['description'] = strip_tags( $services_input['description'] );
                
		update_post_meta( $post_id, SERVICES_META_KEY, $services_input );
                
                $order_number = absint( $services_input['service_order'] );
                
                /*echo "<pre>";
                print_r( $_POST );
                echo "</pre>";
                
                throw new Exception ( 'BAM' );*/
                
		update_post_meta( $post_id, SERVICES_META_ORDER, $order_number );
			
	}
	
}

add_action( 'pre_get_posts', 'reorder_service_posts' );

function reorder_service_posts( $query ) {
       global $pagenow;

       if( is_admin() && $pagenow == 'edit.php') {
               if( $query->query_vars['post_type'] == 'service' ) {
                       $query->set('orderby', 'meta_value_num');
                       $query->set('meta_key', 'services_order');
                       $query->set('order', 'ASC');
               }
       }
}

?>